﻿using LeakView.common;
using LeakView.service;
using LeakView.view.controls;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace LeakView.view
{
    public partial class FormThreshold : Form
    {

        private double maxSC = double.NaN;
        public double MaxSC
        {
            get => maxSC;
        }

        private double minSC = double.NaN;
        public double MinSC
        {
            get => minSC;
        }

        public FormThreshold()
        {
            InitializeComponent();

            Button2_Click(null, null);
            Button3_Click(null, null);

        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            this.textBox1.Enabled = this.checkBox1.Checked;
        }

        private void checkBox2_CheckedChanged(object sender, EventArgs e)
        {
            this.textBox2.Enabled = this.checkBox2.Checked;
        }

        private void checkBox3_CheckedChanged(object sender, EventArgs e)
        {
            this.textBox3.Enabled = this.checkBox3.Checked;
        }

        private void checkBox4_CheckedChanged(object sender, EventArgs e)
        {
            this.textBox4.Enabled = this.checkBox4.Checked;
        }

        private void button1_Click(object sender, EventArgs e)
        {

            double sc_max = double.NaN;
            if (this.checkBox1.Checked)
            {
                try
                {
                    sc_max = double.Parse(this.textBox1.Text);
                }
                catch
                {
                    MessageBox.Show("输差上限阈值格式错误", "错误");
                    return;
                }
               
            }

            double sc_min = double.NaN;
            if (this.checkBox2.Checked)
            {
                try
                {
                    sc_min = double.Parse(this.textBox2.Text);
                }
                catch
                {
                    MessageBox.Show("输差下限阈值格式错误", "错误");
                    return;
                }

            }

            double yc_max = double.NaN;
            if (this.checkBox3.Checked)
            {
                try
                {
                    yc_max = double.Parse(this.textBox3.Text);
                }
                catch
                {
                    MessageBox.Show("压差上限阈值格式错误", "错误");
                    return;
                }

            }

            double yc_min = double.NaN;
            if (this.checkBox4.Checked)
            {
                try
                {
                    yc_min = double.Parse(this.textBox4.Text);
                }
                catch
                {
                    MessageBox.Show("压差下限阈值格式错误", "错误");
                    return;
                }

            }


            IniAnyConfig config = AnyConfigs.get("MISC");
            config.set("SC_MAX", sc_max.ToString());
            config.set("SC_MIN", sc_min.ToString());
            config.set("YC_MAX", yc_max.ToString());
            config.set("YC_MIN", yc_min.ToString());

            //this.DialogResult = System.Windows.Forms.DialogResult.OK;

        }

        private void Button2_Click(object sender, EventArgs e)
        {
            AnyConfig config = AnyConfigs.get("MISC");
            double sc_max = config.get("SC_MAX").GetOrDefault(double.NaN);
            double sc_min = config.get("SC_MIN").GetOrDefault(double.NaN);
            double yc_max = config.get("YC_MAX").GetOrDefault(double.NaN);
            double yc_min = config.get("YC_MIN").GetOrDefault(double.NaN);

            if (double.IsNaN(sc_max))
            {
                this.checkBox1.Checked = false;
            }
            else
            {
                this.checkBox1.Checked = true;
                this.textBox1.Text = sc_max.ToString("F3");
                //this.textBox1.Enabled = true;
            }

            if (double.IsNaN(sc_min))
            {
                this.checkBox2.Checked = false;
            }
            else
            {
                this.checkBox2.Checked = true;
                this.textBox2.Text = sc_min.ToString("F3");
                //this.textBox2.Enabled = true;
            }

            if (double.IsNaN(yc_max))
            {
                this.checkBox3.Checked = false;
            }
            else
            {
                this.checkBox3.Checked = true;
                this.textBox3.Text = yc_max.ToString("F3");
                //this.textBox1.Enabled = true;
            }

            if (double.IsNaN(yc_min))
            {
                this.checkBox4.Checked = false;
            }
            else
            {
                this.checkBox4.Checked = true;
                this.textBox4.Text = yc_min.ToString("F3");
                //this.textBox2.Enabled = true;
            }
        }

        private void Button3_Click(object sender, EventArgs e)
        {
            try
            {
                using (new LoadingStarter())
                {
                    AnyConfig config = AnyConfigs.get("MISC");

                    var vKey = config.get("Head_MCDL").GetOrDefault("");
                    if (vKey.Length > 0)
                    {
                        this.textBoxHeadDL.Text = MiscModal.me.GetFetcherValueParameter(vKey, "valueRatio");
                    }
                    else
                    {
                        this.textBoxHeadDL.Enabled = false;
                    }

                    vKey = config.get("Head_MCDL2").GetOrDefault("");
                    if (vKey.Length > 0)
                    {
                        this.textBoxHeadDL2.Text = MiscModal.me.GetFetcherValueParameter(vKey, "valueRatio");
                    }
                    else
                    {
                        this.textBoxHeadDL2.Enabled = false;
                    }

                    vKey = config.get("Tail_MCDL").GetOrDefault("");
                    if (vKey.Length > 0)
                    {
                        this.textBoxTailDL.Text = MiscModal.me.GetFetcherValueParameter(vKey, "valueRatio");
                    }
                    else
                    {
                        this.textBoxTailDL.Enabled = false;
                    }

                    vKey = config.get("Tail_MCDL2").GetOrDefault("");
                    if (vKey.Length > 0)
                    {
                        this.textBoxTailDL2.Text = MiscModal.me.GetFetcherValueParameter(vKey, "valueRatio");
                    }
                    else
                    {
                        this.textBoxTailDL2.Enabled = false;
                    }

                    vKey = config.get("Head_YLLC").GetOrDefault("");
                    if (vKey.Length > 0)
                    {
                        this.textBoxHeadLCMin.Text = MiscModal.me.GetFetcherValueParameter(vKey, "referMin");
                        this.textBoxHeadLCMax.Text = MiscModal.me.GetFetcherValueParameter(vKey, "referMax");
                    }
                    else
                    {
                        this.textBoxHeadLCMin.Enabled = false;
                        this.textBoxHeadLCMax.Enabled = false;
                    }

                    vKey = config.get("Head_YLLC2").GetOrDefault("");
                    if (vKey.Length > 0)
                    {
                        this.textBoxHeadLCMin2.Text = MiscModal.me.GetFetcherValueParameter(vKey, "referMin");
                        this.textBoxHeadLCMax2.Text = MiscModal.me.GetFetcherValueParameter(vKey, "referMax");
                    }
                    else
                    {
                        this.textBoxHeadLCMin2.Enabled = false;
                        this.textBoxHeadLCMax2.Enabled = false;
                    }

                    vKey = config.get("Tail_YLLC").GetOrDefault("");
                    if (vKey.Length > 0)
                    {
                        this.textBoxTailLCMin.Text = MiscModal.me.GetFetcherValueParameter(vKey, "referMin");
                        this.textBoxTailLCMax.Text = MiscModal.me.GetFetcherValueParameter(vKey, "referMax");
                    }
                    else
                    {
                        this.textBoxTailLCMin.Enabled = false;
                        this.textBoxTailLCMax.Enabled = false;
                    }

                    vKey = config.get("Tail_YLLC2").GetOrDefault("");
                    if (vKey.Length > 0)
                    {
                        this.textBoxTailLCMin2.Text = MiscModal.me.GetFetcherValueParameter(vKey, "referMin");
                        this.textBoxTailLCMax2.Text = MiscModal.me.GetFetcherValueParameter(vKey, "referMax");
                    }
                    else
                    {
                        this.textBoxTailLCMin2.Enabled = false;
                        this.textBoxTailLCMax2.Enabled = false;
                    }
                }
            }
            catch
            {
                MessageBox.Show("获取流量压力参数失败，请确定服务器是否正常！", "错误");
            }

        }

        private void Button4_Click(object sender, EventArgs e)
        {
            AnyConfig config = AnyConfigs.get("MISC");

            var vKey = config.get("Head_MCDL").GetOrDefault("");
            if (vKey.Length > 0)
            {
                var v = double.Parse(this.textBoxHeadDL.Text);
                MiscModal.me.SetFetcherValueParameter(vKey, "valueRatio", v.ToString());
            }

            vKey = config.get("Head_MCDL2").GetOrDefault("");
            if (vKey.Length > 0)
            {
                var v = double.Parse(this.textBoxHeadDL2.Text);
                MiscModal.me.SetFetcherValueParameter(vKey, "valueRatio", v.ToString());
            }

            vKey = config.get("Tail_MCDL").GetOrDefault("");
            if (vKey.Length > 0)
            {
                var v = double.Parse(this.textBoxTailDL.Text);
                MiscModal.me.SetFetcherValueParameter(vKey, "valueRatio", v.ToString());
            }

            vKey = config.get("Tail_MCDL2").GetOrDefault("");
            if (vKey.Length > 0)
            {
                var v = double.Parse(this.textBoxTailDL2.Text);
                MiscModal.me.SetFetcherValueParameter(vKey, "valueRatio", v.ToString());
            }

            vKey = config.get("Head_YLLC").GetOrDefault("");
            if (vKey.Length > 0)
            {
                var v = double.Parse(this.textBoxHeadLCMin.Text);
                MiscModal.me.SetFetcherValueParameter(vKey, "referMin", v.ToString());

                v = double.Parse(this.textBoxHeadLCMax.Text);
                MiscModal.me.SetFetcherValueParameter(vKey, "referMax", v.ToString());
            }

            vKey = config.get("Head_YLLC2").GetOrDefault("");
            if (vKey.Length > 0)
            {
                var v = double.Parse(this.textBoxHeadLCMin2.Text);
                MiscModal.me.SetFetcherValueParameter(vKey, "referMin", v.ToString());

                v = double.Parse(this.textBoxHeadLCMax2.Text);
                MiscModal.me.SetFetcherValueParameter(vKey, "referMax", v.ToString());
            }

            vKey = config.get("Tail_YLLC").GetOrDefault("");
            if (vKey.Length > 0)
            {
                var v = double.Parse(this.textBoxTailLCMin.Text);
                MiscModal.me.SetFetcherValueParameter(vKey, "referMin", v.ToString());

                v = double.Parse(this.textBoxTailLCMax.Text);
                MiscModal.me.SetFetcherValueParameter(vKey, "referMax", v.ToString());
            }

            vKey = config.get("Tail_YLLC2").GetOrDefault("");
            if (vKey.Length > 0)
            {
                var v = double.Parse(this.textBoxTailLCMin2.Text);
                MiscModal.me.SetFetcherValueParameter(vKey, "referMin", v.ToString());

                v = double.Parse(this.textBoxTailLCMax2.Text);
                MiscModal.me.SetFetcherValueParameter(vKey, "referMax", v.ToString());
            }


        }
    }
}
